/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml2postgreconvertor;

import com.sun.xml.internal.ws.util.xml.NodeListIterator;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.xerces.dom.DOMImplementationImpl;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.dom.ElementImpl;
import org.apache.xerces.dom.NodeImpl;
import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author swosh
 */
public class XMLParser {

    /**
     * 
     * @param path
     * @param valRootWan
     * @param nameRootWan
     */
    public List get3XMLAttrValByRootAttrVal(String path, String nameRootWan, String attrRootWan, String valRootWan,
                                      String nameE1Wan, String attrE1Wan, String nameE2Wan, String attrE2Wan) {
        DOMParser parser = new DOMParser();
        NodeImpl rootWan = null;
        List<String[]> listString3 = new ArrayList<String[]>();
        if (path == null || valRootWan == null) {
            return listString3;
        }
        try {
            parser.parse(new InputSource(path));
        } catch (SAXException ex) {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XMLParser.class.getName()).log(Level.SEVERE, null, ex);
        }
        Document doc = parser.getDocument();
        NodeList roots = doc.getElementsByTagName(nameRootWan);

        for (int i = 0; i < roots.getLength(); i++) {
            NodeImpl root = (NodeImpl) roots.item(i);
            NamedNodeMap attrsR = root.getAttributes();
            Node nameWanAR = attrsR.getNamedItem(attrRootWan);
            String valWanAR = nameWanAR.getNodeValue();
            if (valRootWan.equalsIgnoreCase(valWanAR)) {
                rootWan = root;
                break;
            }
        }

        NodeList eles1 = rootWan.getChildNodes();
        for (int i = 0; i < eles1.getLength(); i++) {
            NodeImpl ele1 = (NodeImpl) eles1.item(i);
            NodeImpl ele1Wan;
            if (ele1.getNodeName().equalsIgnoreCase(nameE1Wan)) {
                ele1Wan = ele1;
                NodeList eles2 = ele1Wan.getChildNodes();
                for (int j = 0; j < eles2.getLength(); j++) {
                    NodeImpl ele2 = (NodeImpl) eles2.item(j);
                    NodeImpl ele2Wan;
                    if (ele2.getNodeName().equalsIgnoreCase(nameE2Wan)) {
                        ele2Wan = ele2;
                        String[] arrayString = new String[3];
                        arrayString[0] = rootWan.getAttributes().getNamedItem(attrRootWan).getNodeValue();
                        arrayString[1] = ele1Wan.getAttributes().getNamedItem(attrE1Wan).getNodeValue();
                        arrayString[2] = ele2Wan.getAttributes().getNamedItem(attrE2Wan).getNodeValue();
                        listString3.add(arrayString);
                    }
                }
            }
        }
        return listString3;
    }
}